<?php
/**
 * Created By PhpStorm
 * Author: patrick wu
 * Date: 2020/9/19
 * Time: 13:18
 */
namespace App\Http\Middleware;

use App\Constant\ErrorConstant;
use App\MultiTrait\ResponseTrait;

class AdminCheckMiddleware
{
    use ResponseTrait;
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, \Closure $next)
    {
        /**
         * 检测header头或者post体中带有token
         */
        if (empty($user = auth('admin')->user())){
            return $this->errorResponse(ErrorConstant::TOKEN_ERROR);
        }

        $admin_user['username'] =   $user->username;
        $admin_user['user_id']  =   $user->id;
        $admin_user['phone']    =   $user->phone;
        $admin_user['email']    =   $user->email;
        $request->merge(['admin_user' => $admin_user]);
        return $next($request);
    }
}
